﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Kokomo
{
	/// <summary>
	/// Provides extension methods for arrays.
	/// </summary>
	public static class ArrayExtension
	{
		//public static IEnumerable<TElement> Append<TElement>(this IEnumerable<TElement> sequence, TElement element)
		//{
		//    return sequence.Concat(new TElement[] { element });
		//}
		//public static bool ElementsEqual<TElement>(this TElement[] array, TElement[] array2)
		//{
		//    ArgumentValidation.CheckNullArgument("array", array);
		//    if ((array2 == null) || (array2.Length != array.Length))
		//    {
		//        return false;
		//    }

		//    int count = array.Length;
		//    for (int i = 0; i < array.Length; i++)
		//    {
		//        TElement element1 = array[i];
		//        TElement element2 = array2[i];
		//        bool equal = object.Equals(element1, element2);

		//        if (!equal)
		//        {
		//            return false;
		//        }
		//    }

		//    return true;
		//}
		//public static int GetElementHashCode<TElement>(this IEnumerable<TElement> array)
		//{
		//    ArgumentValidation.CheckNullArgument("array", array);

		//    int hash = 0;
		//    foreach (var item in array)
		//    {
		//        int elementHash = ((item == null) ? 0 : item.GetHashCode());
		//        hash ^= elementHash;
		//    }

		//    return hash;
		//}
		//public static string ToCommaSeparatedListString(this IEnumerable<object> array)
		//{
		//    ArgumentValidation.CheckNullArgument("array", array);

		//    //string[] strings = Array.ConvertAll(array, element => element.ToString());
		//    string list = string.Join(",", array);

		//    return list;
		//}
		public static TStruct ToStruct<TStruct>(this byte[] array) where TStruct : struct
		{
			ArgumentValidation.CheckNullArgument("array", array);
			int structSize = typeof(TStruct).SizeOf();
			int copySize = Math.Min(structSize, array.Length);

			TStruct @struct;
			using (UnmanagedBuffer buffer = UnmanagedBuffer.Allocate(structSize))
			{
				buffer.Write(array);
				@struct = buffer.ReadStruct<TStruct>();
			}

			return @struct;
		}
	}
}
